﻿// CChangePictureDlg.cpp: 实现文件
//

#include "pch.h"
#include "RandomRollCall.h"
#include "CChangePictureDlg.h"
#include"RandomRollCallDlg.h"
#include "afxdialogex.h"


// CChangePictureDlg 对话框

IMPLEMENT_DYNAMIC(CChangePictureDlg, CDialogEx)

CChangePictureDlg::CChangePictureDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG2, pParent)
{

}

CChangePictureDlg::~CChangePictureDlg()
{
}

void CChangePictureDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, m_TextPath);
	DDX_Control(pDX, IDCANCEL, m_PicShow);
}
//
//void CChangePictureDlg::OnPaint()
//{
//	CWnd* pWnd;
//	pWnd = GetDlgItem(IDC_STATIC);
//	CDC* pDC = pWnd->GetDC();
//	HDC hDC = pDC->m_hDC;
//
//	CRect rect_frame;
//	pWnd->GetClientRect(&rect_frame);
//
//	::SetStretchBltMode(hDC, HALFTONE);
//	::SetBrushOrgEx(hDC, 0, 0, NULL);
//
//	m_TargetImg.Draw(hDC, rect_frame);
//	ReleaseDC(pDC);
//	::OnPaint();
//}


BEGIN_MESSAGE_MAP(CChangePictureDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &CChangePictureDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CChangePictureDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDOK, &CChangePictureDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CChangePictureDlg 消息处理程序


void CChangePictureDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString& str = getEditText();
	if (!m_TargetImg.IsNull())
	{
		m_TargetImg.Destroy();
	}
	if (m_TargetImg.Load(str) != S_OK)
	{
		MessageBox(_T("图片加载错误！"), _T("错误"));
		return;
	}

	DrawImage();
}

void CChangePictureDlg::DrawImage()
{
	//绘制图片
	CWnd* pWnd;
	pWnd = GetDlgItem(IDC_STATIC);
	CDC* pDC = pWnd->GetDC();
	HDC hDC = pDC->m_hDC;

	CRect rect_frame;
	pWnd->GetClientRect(&rect_frame);

	::SetStretchBltMode(hDC, HALFTONE);
	::SetBrushOrgEx(hDC, 0, 0, NULL);

	m_TargetImg.Draw(hDC, rect_frame);
	ReleaseDC(pDC);
}


CString CChangePictureDlg::getEditText()
{
	UpdateData(TRUE);
	CString str;
	m_TextPath.GetWindowTextW(str);
	UpdateData(FALSE);
	return str;
}


void CChangePictureDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("JPG文件|*.jpg |PNG文件|*.png|BMP文件|*.bmp|任意文件|*.*|"), NULL);

	if (dlgFile.DoModal()==IDOK)
	{
		UpdateData(TRUE);
		m_TextPath.SetWindowTextW(dlgFile.GetFolderPath()+_T("\\")+dlgFile.GetFileName());
		UpdateData(FALSE);
		CString& str = getEditText();
		if (!m_TargetImg.IsNull())
		{
			m_TargetImg.Destroy();
		}
		if (m_TargetImg.Load(str) != S_OK)
		{
			MessageBox(_T("图片加载错误！"), _T("错误"));
			return;
		}
		DrawImage();
	}

}


void CChangePictureDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	//if (DeleteFile(Manager::ProgramPath::Path() + CRandomRollCallDlg::NAME_PICBACKGROUND()))
	//{
	//	m_TargetImg.Save(Manager::ProgramPath::Path() + /*MIDSAVEPICNAME*/CRandomRollCallDlg::NAME_PICBACKGROUND());
	//}

	m_TargetImg.Save(Manager::ProgramPath::Path() + MIDSAVEPICNAME/*CRandomRollCallDlg::NAME_PICBACKGROUND()*/);
	CDialogEx::OnOK();
}

CString CChangePictureDlg::NAME_MIDSAVEPICNAME()
{
	return CString(MIDSAVEPICNAME);
}
